/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package devbureau.fstore.common.print.report;

import devbureau.fstore.common.print.PrintException;
import devbureau.fstore.common.report.Bill;
import devbureau.fstore.util.Utilities;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import static devbureau.fstore.util.ExcelHelper.*;
/**
 *
 * @author SSheypak
 */
public class PrintBillReport {
    private static final Logger log = Logger.getLogger(PrintBillReport.class);
    
    private static final String defaultPathToTemplateFile = "bill_report_template.xls";
    private static final int ROW_NUM = 7;

    private List<Bill> bills = null;
    private OutputStream resultStream = null;


    public PrintBillReport(List<Bill> bills, OutputStream resultStream){
        this.bills = bills;
        this.resultStream = resultStream;
    }

    /**Writes report to output stream*/
    public void writeReport() throws PrintException{
        log.debug("writeReport outStream["+resultStream+"] billsSize["+bills.size()+"]");
        InputStream xlsIS = null;
        try{
            xlsIS = this.getClass().getClassLoader().getResourceAsStream(defaultPathToTemplateFile);
            HSSFWorkbook workBook = new HSSFWorkbook(xlsIS);
            HSSFSheet sheet = workBook.getSheetAt(0);

            HSSFCellStyle style = workBook.createCellStyle();
            style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
            Double totalPrice = 0D;
            for(int i=0; i< bills.size(); i++){
                Bill bill = bills.get(i);
                String[] values = bill.getValuesForXLSReport();
                HSSFRow row = sheet.createRow(ROW_NUM+i);
                totalPrice+=bill.getPercent();
                for(int j=0; j<values.length; j++){
                    HSSFCell cell = row.createCell(j);
                    cell.setCellValue(convert( values[j] ));
                    cell.setCellStyle(style);
                }
            }
            HSSFRow totalRow = sheet.createRow(ROW_NUM+bills.size());
            HSSFCell totalCellTitle = totalRow.createCell(3);
            totalCellTitle.setCellValue(convert("Итого: "));
            HSSFCell totalCellValue = totalRow.createCell(4);
            totalRow.createCell(5);
            totalCellValue.setCellValue(convert(Utilities.Formatting.formatPrice(totalPrice)+" руб."));
            totalCellValue.setCellStyle(style);
            sheet.addMergedRegion(new CellRangeAddress(ROW_NUM+bills.size(), ROW_NUM+bills.size(), 4, 5));
            workBook.write(resultStream);
        }catch(Exception e){
            log.error("Error while creating BillReport xls.", e);
            throw new PrintException("Произошла ошибка при создании excel файла отчёта. Отчёт не создан.");
        }finally{
            if(xlsIS!=null){
                try{
                xlsIS.close();
                }catch(Exception e){
                    log.error("Error while closing input stream. " +e.getMessage());
                }
            }
        }
    }
}
